package com.travel.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianfeng.util.ResultMsg;
import com.travel.entity.TbCart;
import com.travel.entity.TbHome;
import com.travel.entity.TbTripOrder;
import com.travel.entity.TbUser;
import com.travel.exception.OrderAddFailureException;
import com.travel.mapper.CartMapper;
import com.travel.mapper.TripOrderMapper;
import com.travel.mapper.UserMapper;
import com.travel.service.TripOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class TripOrderServiceImpl extends ServiceImpl<TripOrderMapper, TbTripOrder> implements TripOrderService {
    @Autowired
    TripOrderMapper tripOrderMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    CartMapper cartMapper;



    /**
     * 添加车票订单
     * */
    @Transactional(rollbackFor = OrderAddFailureException.class)
    @Override
    public ResultMsg addCartOrder(TbCart tbCart, TbUser tbUser) {
        TbTripOrder tbTripOrder = new TbTripOrder();
        Snowflake snowflake = IdUtil.getSnowflake(20, 20);
        String s = snowflake.nextIdStr();
        tbTripOrder.setTripTicketId(s);
        tbTripOrder.setUserId(tbUser.getUserId());
        tbTripOrder.setUserRname(tbUser.getUserName());
        tbTripOrder.setUserPhone(tbUser.getUserTel());
        tbTripOrder.setUserCarid(tbCart.getCarId().toString());
        tbTripOrder.setTripTicketTime(new Date());
        tbTripOrder.setTripTicketStatus(0);
        tbTripOrder.setTripTicketFlag("汽车票");
        tbTripOrder.setTripTicketTotal(tbCart.getCarPrice());
        boolean saveOrder = super.save(tbTripOrder);
        /*tbTripOrder.setTripTicketStatus(1);
        int updateTbTripOrder = tbTripOrder.updateById(tbTripOrder);*/
        if (saveOrder==true /*&& updateTbTripOrder==1*/){
            return ResultMsg.success(200);
        }
        throw new OrderAddFailureException();
        /*UUID.randomUUID().toString()*/

    }
    /**
     * 查询所有车票信息
     * */
    @Override
    public ResultMsg getAllTripOrder(int current,int size) {
        Page<TbTripOrder> ipage = new Page<>(current,size);
        Page<TbTripOrder> page = super.page(ipage);
        return ResultMsg.success(page);

    }
    /**
     * 通过id查询车票订单
     * */
    @Override
    public ResultMsg getTripOrderById(String tripTicketId) {
        TbTripOrder byId = super.getById(tripTicketId);
        if(byId==null){
            return  ResultMsg.error(555,"没有当前的汽车班次");
        }else {
            return ResultMsg.success(byId);
        }
        /*QueryWrapper<TbTripOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("",tripTicketId);
        wrapper.eq("",userId);
        wrapper.eq("",userRname);
        wrapper.eq("",userPhone);
        wrapper.eq("",userCarid);
        wrapper.eq("",tripTicketTime);
        wrapper.eq("",tripTicketStatus);
        wrapper.eq("",tripTicketFlag);
        wrapper.eq("",tripTicketTotal);
        List<TbTripOrder> list = super.list(wrapper);
        if (list!=null){
            return ResultMsg.success(list);
        }
        return ResultMsg.error(300,"没有房间");*/
    }
    /**
     * 修改车票订单
     * */
    @Override
    public ResultMsg updateTripOrder(TbTripOrder tbTripOrder) {
        boolean b = super.saveOrUpdate(tbTripOrder);
        if (tbTripOrder!=null)
            return ResultMsg.success(b);
        else
            return ResultMsg.error(555,"修改汽车票订单失败");
    }
    /**
     * 添加车票订单
     * */
    @Override
    public ResultMsg addTripOrder(TbTripOrder tbTripOrder) {
        boolean b = super.saveOrUpdate(tbTripOrder);
        if (tbTripOrder!=null)
            return ResultMsg.success(b);
        else
            return ResultMsg.error(555,"添加汽车票订单失败");
    }
    /**
     * 删除车票订单
     * */
    @Override
    public ResultMsg deleteTripOrder(int id) {
        boolean b = super.removeById(id);
        if (b)
            return ResultMsg.success(b);
        return ResultMsg.error(555,"删除订单失败");
    }
    /**
     * 添加出行订单汽车车票订单
     * */
    @Override
    public ResultMsg carOrder(TbTripOrder tbTripOrder) {
        tbTripOrder.setTripTicketId(UUID.randomUUID().toString());
        tbTripOrder.setTripTicketTime(new Date());
        tbTripOrder.setTripTicketFlag("汽车票");
        tbTripOrder.setTripTicketStatus(0);
        int insert = tripOrderMapper.insert(tbTripOrder);
        if (insert==1){
            return ResultMsg.success();
        }
        return ResultMsg.error(604,"汽车票订单添加失败");
    }
    /**
     * 添加出行订单飞机票订单
     * */
    @Override
    public ResultMsg addAirOrder(TbTripOrder tbTripOrder) {
        tbTripOrder.setTripTicketId(UUID.randomUUID().toString());
        tbTripOrder.setTripTicketTime(new Date());
        tbTripOrder.setTripTicketFlag("飞机票");
        tbTripOrder.setTripTicketStatus(0);
        int insert = tripOrderMapper.insert(tbTripOrder);
        if (insert==1){
            return ResultMsg.success();
        }
        return ResultMsg.error(601,"机票订单添加失败");
    }
    /**
     * 查询出行订单所有车票信息
     * */
    @Override
    public ResultMsg findAllOrder(int current, int size) {
        Page<TbTripOrder> tbTripOrderPage = new Page<>(current, size);
        Page<TbTripOrder> page = super.page(tbTripOrderPage);
        return ResultMsg.success(page);
    }
    /**
     * 删除出行订单车票订单
     * */
    @Override
    public ResultMsg deleteByOne(String tripTicketId) {
        boolean b = super.removeById(tripTicketId);
        if(b){
            return ResultMsg.success();
        }
        return ResultMsg.error(604,"方法错误");
    }

}
